<template>
  <el-dialog :visible.sync="isShow" :before-close="handleCancelModal" :close-on-click-modal="false" width="500px">
    <span slot="title" v-text="formParams['bid_id'] ? '编辑场次信息' : '创建场次'" />
    <!-- 数据表单 -->
    <el-form ref="formParams" :model="formParams" label-width="120px" size="small" style="padding-right: 40px">
      <el-form-item class="is-required" prop="bid_code" label="场次代码：">
        <el-input v-model="formParams.bid_code" placeholder="请输入不重复的场次代码" />
      </el-form-item>
      <el-form-item class="is-required" prop="opened_at" label="开场时间：">
        <el-date-picker
          v-model="formParams.opened_at"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择开场时间"
        />
      </el-form-item>
      <el-form-item class="is-required" prop="ended_at" label="结束时间：">
        <el-date-picker
          v-model="formParams.ended_at"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择结束时间"
        />
      </el-form-item>
      <el-form-item prop="bid_name" label="场次名称：">
        <el-input v-model="formParams.bid_name" placeholder="请输入场次名称" />
      </el-form-item>
      <el-form-item prop="remarks" label="场次备注：">
        <el-input
          v-model="formParams.remarks"
          type="textarea"
          :rows="2"
          placeholder="请输入场次备注"
        />
      </el-form-item>
      <el-form-item prop="status" label="场次状态：">
        <el-radio-group v-model="formParams.status" size="mini">
          <el-radio-button :label="0"><span style="padding: 0 5px">已关闭</span></el-radio-button>
          <el-radio-button :label="1"><span style="padding: 0 5px">进行中</span></el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 提交按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button size="small" :disabled="isSubmit" @click="handleCancelModal">取 消</el-button>
      <el-button type="primary" size="small" :loading="isSubmit" @click="handleSubmitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { save } from '@/api/bid'
import { extract } from '@/utils/tools'

export default {
  props: {
    'isShow': { type: Boolean, default: true },
    'isUpdate': { type: Boolean, default: false },
    'itemData': { type: Object, default: null },
    'option': { type: Object, default: null }
  },
  data: function() {
    return {
      isSubmit: false,
      formParams: {
        bid_id: null,
        bid_code: null,
        bid_name: null,
        remarks: null,
        opened_at: null,
        ended_at: null,
        status: 0
      }
    }
  },
  mounted: function() {
    if (this.itemData) {
      this.itemData.bid_id = this.itemData.bid_id ? this.itemData.bid_id : null
      this.formParams = extract(this.formParams, this.itemData)
    }
  },
  methods: {
    handleSubmitForm() {
      console.log(this.formParams)
      this.isSubmit = true
      save(this.formParams, this.formParams['bid_id'] || null).then(function() {
        this.$emit('on-cancel-modal', true)
      }.bind(this)).catch(function() {
        this.isSubmit = false
      }.bind(this))
    },
    // 取消弹出框
    handleCancelModal(refresh = false) {
      if (!this.isSubmit) {
        this.$emit('on-cancel-modal', refresh)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.partition-container {
  display: flex;
  .partition-left {
    flex-grow: 1;
    margin-right: 15px;
  }
  .partition-right {
    width: 200px;
    height: 262px;
    border: 1px solid #DCDFE6;
    .title {
      height: 31px;
      font-size: 14px;
      font-weight: 700;
      line-height: 31px;
      padding-left: 8px;
      background-color: #f2f2f2;
    }
    .rule-main {
      height: 227px;
      margin-top: 1px;
      overflow-y: scroll;
      .el-tree {
        margin-top: 5px;
      }
    }
  }
}
</style>
